package com.hitqz.robot.biz.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hitqz.robot.api.business.enums.BrandEnum;
import com.hitqz.robot.api.business.enums.EquipmentEnum;
import com.hitqz.robot.api.business.model.vo.robot.RobotEquipmentBrandVo;
import com.hitqz.robot.api.business.model.vo.robot.RobotEquipmentTypeVo;
import com.hitqz.robot.api.business.model.vo.robot.RobotEquipmentVo;
import com.hitqz.robot.api.common.entity.RobotEquipment;
import com.hitqz.robot.biz.service.RobotEquipmentService;
import com.hitqz.robot.camera.EquipmentFactory;
import com.hitqz.robot.camera.IEquipmentService;
import com.hitqz.robot.camera.dto.EquipmentParamsDto;
import com.hitqz.robot.camera.dto.QueryParamsDto;
import com.hitqz.robot.common.core.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author wengyx
 * @date 2024/8/7
 */

@RestController
@AllArgsConstructor
@RequestMapping("/robotEquipment")
@Tag(name = "机器人外设管理")
public class RobotEquipmentController {

    private final RobotEquipmentService robotEquipmentService;

    private final EquipmentFactory equipmentFactory;

    @GetMapping("/list" )
    @Operation(summary = "查询机器人外设列表")
    public R<List<RobotEquipmentVo>> list() {
        List<RobotEquipment> list = robotEquipmentService.list();
        List<RobotEquipmentVo> voList = BeanUtil.copyToList(list, RobotEquipmentVo.class);
        return R.ok(voList);
    }


    @GetMapping("/listEquipmentEnum/oilPlc" )
    @Operation(summary = "单独读取取油柜-PLC数据")
    public R<List<RobotEquipmentVo>> listEquipmentEnum() {
        LambdaQueryWrapper<RobotEquipment> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RobotEquipment::getBrand,  BrandEnum.OIL_PLC.getEnStr());
        List<RobotEquipment> list = robotEquipmentService.list(wrapper);
        List<RobotEquipmentVo> voList = BeanUtil.copyToList(list, RobotEquipmentVo.class);
        return R.ok(voList);
    }





    @GetMapping("/{id}" )
    @Operation(summary = "根据id查询机器人外设")
    public R<RobotEquipmentVo> getById(@PathVariable("id") Integer id) {
        RobotEquipment robotEquipment = robotEquipmentService.getById(id);
        RobotEquipmentVo vo = BeanUtil.copyProperties(robotEquipment, RobotEquipmentVo.class);
        return R.ok(vo);
    }


    @PostMapping
    @Operation(summary = "新增机器人外设")
    public R save(@RequestBody @Validated RobotEquipment dto) {
        return R.ok(robotEquipmentService.save(dto));
    }

    @PutMapping
    @Operation(summary = "修改机器人外设")
    public R updateById(@RequestBody @Validated RobotEquipment dto) {
        return R.ok(robotEquipmentService.updateById(dto));
    }

    @DeleteMapping
    @Operation(summary = "根据id删除机器人外设")
    public R removeById(@RequestBody Integer[] ids) {
        return R.ok(robotEquipmentService.removeBatchByIds(CollUtil.toList(ids)));
    }

    @GetMapping("/options/type" )
    @Operation(summary = "查询机器人外设种类选项")
    public R<List<RobotEquipmentTypeVo>> type() {
        List<RobotEquipmentTypeVo> list = new ArrayList<>();
        for (Integer i : EquipmentEnum.TYPE_DES_MAP.keySet()) {
            RobotEquipmentTypeVo vo = new RobotEquipmentTypeVo();
            vo.setType(i);
            vo.setTypeStr(EquipmentEnum.TYPE_DES_MAP.get(i));
            list.add(vo);
        }
        return R.ok(list);
    }

    @GetMapping("/options/brand" )
    @Operation(summary = "查询某种外设底下的品牌选项")
    public R<List<RobotEquipmentBrandVo>> brand(@RequestParam(value = "type",required = false)Integer type) {
        List<BrandEnum> brandEnums = Arrays.stream(BrandEnum.values()).toList();
        if (type!=null) {
            brandEnums = EquipmentEnum.TYPE_BRAND_MAP.get(type);
        }
        List<RobotEquipmentBrandVo> list = new ArrayList<>();
        for (BrandEnum brandEnum :brandEnums) {
            RobotEquipmentBrandVo robotEquipmentBrandVo = new RobotEquipmentBrandVo();
            robotEquipmentBrandVo.setBrand(brandEnum.getEnStr());
            robotEquipmentBrandVo.setBrandStr(brandEnum.getCnStr());
            list.add(robotEquipmentBrandVo);
        }
        return R.ok(list);
    }

    @GetMapping("/playList" )
    @Operation(summary = "查询支持预览外设")
    public R<List<RobotEquipmentVo>> playList(){
        List<RobotEquipment> list = robotEquipmentService.list(Wrappers.<RobotEquipment>lambdaQuery().eq(RobotEquipment::getIsPlay, 1));
        List<RobotEquipmentVo> voList = BeanUtil.copyToList(list, RobotEquipmentVo.class);
        return R.ok(voList);
    }

    @GetMapping("/recordList" )
    @Operation(summary = "查询支持标定参数外设")
    public R<List<RobotEquipmentVo>> recordList(){
        List<RobotEquipment> list = robotEquipmentService.list(Wrappers.<RobotEquipment>lambdaQuery().eq(RobotEquipment::getIsRecordParam, 1));
        List<RobotEquipmentVo> voList = BeanUtil.copyToList(list, RobotEquipmentVo.class);
        return R.ok(voList);
    }


    @GetMapping("/detectList" )
    @Operation(summary = "查询支持检测外设")
    public R<List<RobotEquipmentVo>> detectList(){
        List<RobotEquipment> list = robotEquipmentService.list(Wrappers.<RobotEquipment>lambdaQuery().eq(RobotEquipment::getIsDetect, 1));
        List<RobotEquipmentVo> voList = BeanUtil.copyToList(list, RobotEquipmentVo.class);
        return R.ok(voList);
    }


    @GetMapping("/{id}/currentPose" )
    @Operation(summary = "根据id查询机器人外设当前参数")
    public R<String> getCurrentPose(@PathVariable("id") Integer id) {
        RobotEquipment robotEquipment = robotEquipmentService.getById(id);
        IEquipmentService service = equipmentFactory.getServiceByBrand(robotEquipment.getBrand());
        if (service==null){
            return R.failed("所选外设不支持");
        }
        QueryParamsDto dto = new QueryParamsDto();
        dto.setIp(robotEquipment.getIp());
        dto.setUsername(robotEquipment.getUsername());
        dto.setPassword(robotEquipment.getPassword());
        dto.setPort(robotEquipment.getPort());
        dto.setChannel(robotEquipment.getChannel());
        List<EquipmentParamsDto> params = service.getEquipmentParams(dto);
        if (params.isEmpty()){
            return R.failed("无当前外设参数");
        }
        return R.ok(JSON.toJSONString(params));
    }

}
